package com.bsth.repository.schedule;

import com.bsth.entity.schedule.CarConfigInfo;
import com.bsth.repository.BaseRepository;

import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

/**
 * Created by xu on 16/5/9.
 */
@Repository
public interface CarConfigInfoRepository extends BaseRepository<CarConfigInfo, Long> {

//    @EntityGraph(value = "carConfigInfo_xl_cl", type = EntityGraph.EntityGraphType.FETCH)
    // 因为使用了in操作符，用 @EntityGraph会报错，升到hibernate 5.0.4就好了，暂时不用@EntityGraph
    // Hibernate ORMHHH-9230
    @Override
    Page<CarConfigInfo> findAll(Specification<CarConfigInfo> spec, Pageable pageable);

    @EntityGraph(value = "carConfigInfo_xl_cl", type = EntityGraph.EntityGraphType.FETCH)
    @Override
    List<CarConfigInfo> findAll(Specification<CarConfigInfo> spec);

    @EntityGraph(value = "carConfigInfo_xl_cl", type = EntityGraph.EntityGraphType.FETCH)
    @Override
    List<CarConfigInfo> findAll();

    @EntityGraph(value = "carConfigInfo_xl_cl", type = EntityGraph.EntityGraphType.FETCH)
    @Query("select cc from CarConfigInfo cc where cc.id=?1")
    CarConfigInfo findOneExtend(Long aLong);


    @EntityGraph(value = "carConfigInfo_xl_cl", type = EntityGraph.EntityGraphType.FETCH)
	@Query("select cc from CarConfigInfo cc where cc.xl.lineCode=?1")
	List<CarConfigInfo> findBylineCode(String lineCode);

    @Query("select new map(cc.cl.insideCode as nbbm, cc.id as id) from CarConfigInfo cc")
    List<Map<String, Object>> findCarConfigCars();

    @Query("select new map(cc.cl.id as id, cc.cl.insideCode as insideCode) from CarConfigInfo cc")
    List<Map<String, Object>> findCarsFromConfig();

    @EntityGraph(value = "carConfigInfo_xl_cl", type = EntityGraph.EntityGraphType.FETCH)
    List<CarConfigInfo> findByXlId(Integer xlid);
}